package com.common.lib.manager

import android.graphics.Bitmap
import android.text.TextUtils
import com.blankj.utilcode.util.StringUtils
import com.common.lib.bean.ApplicationBean
import com.common.lib.bean.BannerBean
import com.common.lib.bean.UserBean
import com.common.lib.bean.VideoBean
import com.common.lib.utils.PrefUtil
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


class DataManager private constructor() {


    private val mGson = Gson()

    private var mMyInfo: UserBean? = null

    private var mObject: Any? = null


    private  var  mBluethMac=null;

    companion object {
        const val TAG = "DataManager"

        @Volatile
        private var instance: DataManager? = null

        fun getInstance() =
            instance ?: synchronized(this) {
                instance
                    ?: DataManager()
                        .also { instance = it }
            }
    }

    fun getGson(): Gson {
        return mGson
    }


    fun getToken(): String? {
        return PrefUtil.getString(
            ConfigurationManager.getInstance().getContext(),
            "token", ""
        )
    }

    fun  getBlietoothMac () :String?{
        return PrefUtil.getString(
            ConfigurationManager.getInstance().getContext(),
            "mac",""
        )
    }

    fun saveToken(token: String?) {
        PrefUtil.putString(
            ConfigurationManager.getInstance().getContext(),
            "token", token ?: ""
        )
    }

    fun  saveBluetoothMac(mac:String?){
        PrefUtil.putString(
            ConfigurationManager.getInstance().getContext(),
            "mac",mac ?:""
        )
    }

    fun saveHomeApk(datas : ArrayList<ApplicationBean>?){
        if (datas == null) {
            PrefUtil.putString(ConfigurationManager.getInstance().getContext(), "home_apk", "")
            return
        }
        PrefUtil.putString(
            ConfigurationManager.getInstance().getContext(),
            "home_apk",
            mGson.toJson(datas)
        )
    }

    fun getHomeApk(): String? {
        return PrefUtil.getString(ConfigurationManager.getInstance().getContext(), "home_apk", "");
    }


    fun saveMyInfo(myInfo: UserBean?) {
        if (myInfo == null) {
            mMyInfo = null
            PrefUtil.putString(ConfigurationManager.getInstance().getContext(), "user", "")
            return
        }
        mMyInfo = myInfo
        PrefUtil.putString(
            ConfigurationManager.getInstance().getContext(),
            "user",
            mGson.toJson(myInfo)
        )
    }

    fun getMyInfo(): UserBean? {
        if (mMyInfo == null) {
            val str =
                PrefUtil.getString(ConfigurationManager.getInstance().getContext(), "user", "")
            if (TextUtils.isEmpty(str)) {
                return null
            }
            mMyInfo = mGson.fromJson(str, UserBean::class.java)
        }
        return mMyInfo
    }

    fun isLogin(): Boolean {
        return getMyInfo() != null
    }

    fun saveLanguage(language: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "language",
            language
        )
    }

    fun getLanguage(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "language",
            0
        )
    }

    fun saveRegion(region: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "region",
            region
        )
    }


    fun getRegion(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "region",
            0
        )
    }

    fun saveUnit(isUseUnit: Boolean) {
        PrefUtil.putBoolean(
            ConfigurationManager.getInstance().getContext(),
            "isUseUnit",
            isUseUnit
        )
    }


    fun getUnit(): Boolean {
        return PrefUtil.getBoolean(
            ConfigurationManager.getInstance().getContext(),
            "isUseUnit",
            false
        )
    }

    fun saveScreenTime(time: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "screenTime",
            time
        )
    }


    fun getScreenTime(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "screenTime",
            0
        )
    }

    fun saveMainStyle(style: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "mainStyle",
            style
        )
    }


    fun getMainStyle(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "mainStyle",
            0
        )
    }

    fun saveMainTheme(theme: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "mainTheme",
            theme
        )
    }


    fun getMainTheme(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "mainTheme",
            0
        )
    }

    fun saveScreenProtectStyle(style: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "screenProtectStyle",
            style
        )
    }


    fun getScreenProtectStyle(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "screenProtectStyle",
            0
        )
    }


    fun saveTapTime(time: Long) {
        PrefUtil.putLong(
            ConfigurationManager.getInstance().getContext(),
            "tapTime",
            time
        )
    }


    fun getTapTime(): Long {
        return PrefUtil.getLong(
            ConfigurationManager.getInstance().getContext(),
            "tapTime",
            0L
        )
    }

    fun saveIsAutoUpdate(isAutoUpdate: Boolean) {
        PrefUtil.putBoolean(
            ConfigurationManager.getInstance().getContext(),
            "isAutoUpdate",
            isAutoUpdate
        )
    }


    fun getIsAutoUpdate(): Boolean {
        return PrefUtil.getBoolean(
            ConfigurationManager.getInstance().getContext(),
            "isAutoUpdate",
            false
        )
    }

    fun saveMaxSpeed(max: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "max_speed",
            max
        )
    }


    fun getMaxSpeed(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "max_speed",
            160
        )
    }

    fun saveMinSpeed(min: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "min_speed",
            min
        )
    }


    fun getMinSpeed(): String {
        return PrefUtil.getString(
            ConfigurationManager.getInstance().getContext(),
            "min_speed",
            "1.0"
        )
    }

    fun saveMaxSlop(max: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "max_slop",
            max
        )
    }


    fun getMaxSlop(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "max_slop",
            15
        )
    }

    fun saveMinSlop(min: Int) {
        PrefUtil.putInt(
            ConfigurationManager.getInstance().getContext(),
            "min_slop",
            min
        )
    }

    fun getMinSlop(): Int {
        return PrefUtil.getInt(
            ConfigurationManager.getInstance().getContext(),
            "min_slop",
            0
        )
    }

    fun saveVideoList(type: Int, index: Int, list: ArrayList<VideoBean>?) {
        PrefUtil.putString(
            ConfigurationManager.getInstance().getContext(),
            "video_list_" + type + "_" + index, if (list == null) {
                ""
            } else {
                mGson.toJson(list)
            }
        )
    }

    fun getVideoList(type: Int, index: Int): ArrayList<VideoBean> {
        val str =
            PrefUtil.getString(
                ConfigurationManager.getInstance().getContext(),
                "video_list_" + type + "_" + index,
                ""
            )
        return if (TextUtils.isEmpty(str)) {
            ArrayList<VideoBean>()
        } else {
            mGson.fromJson<ArrayList<VideoBean>>(
                str,
                object : TypeToken<ArrayList<VideoBean>>() {}.type
            )
        }
    }


    fun saveBannerList(list: ArrayList<BannerBean>?) {
        PrefUtil.putString(
            ConfigurationManager.getInstance().getContext(),
            "banner_list", if (list == null) {
                ""
            } else {
                mGson.toJson(list)
            }
        )
    }

    fun getBannerList(): ArrayList<BannerBean> {
        val str =
            PrefUtil.getString(
                ConfigurationManager.getInstance().getContext(),
                "banner_list",
                ""
            )
        return if (TextUtils.isEmpty(str)) {
            ArrayList<BannerBean>()
        } else {
            mGson.fromJson<ArrayList<BannerBean>>(
                str,
                object : TypeToken<ArrayList<BannerBean>>() {}.type
            )
        }
    }

    fun saveSetTarget(list: ArrayList<Float>?) {
        PrefUtil.putString(
            ConfigurationManager.getInstance().getContext(),
            "set_target_list", if (list == null) {
                ""
            } else {
                mGson.toJson(list)
            }
        )
    }

    fun getSetTarget(): ArrayList<Float> {
        val str =
            PrefUtil.getString(
                ConfigurationManager.getInstance().getContext(),
                "set_target_list",
                ""
            )
        return if (TextUtils.isEmpty(str)) {
            ArrayList<Float>()
        } else {
            mGson.fromJson<ArrayList<Float>>(
                str,
                object : TypeToken<ArrayList<Float>>() {}.type
            )
        }
    }

    fun setObject(o: Any?) {
        if (o == null && mObject != null && mObject is Bitmap) {
            (mObject as Bitmap).recycle()
        }
        mObject = o
    }

    fun getObject(): Any? {
        return mObject
    }


    fun logout() {
        saveToken("")
        saveMyInfo(null)
        if (mObject != null && mObject is Bitmap) {
            (mObject as Bitmap).recycle()
        }
        mObject = null
    }
}